package com.sensor.ui;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.landmesure.bluetoothchat.R;
import com.landmesure.bluetoothchat.databinding.ActivityWaterMonitorBinding;
import com.sensor.Fragment.InstallationFragment;
import com.sensor.Fragment.JointDebugFragment;
import com.sensor.Fragment.LiveDataFragment;
import com.sensor.Fragment.ParameterSetFragment;

import java.util.ArrayList;
import java.util.List;

public class WaterMonitorActivity extends AppCompatActivity implements View.OnClickListener {

    // TabLayout
    private TabLayout mTabLayout;
    // ViewPager
    private ViewPager mViewPager;
    // Title
    private List<String> mTitle;


    // Fragment
    private static List<Fragment> mFragment;

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityWaterMonitorBinding activityWaterMonitorBinding= ActivityWaterMonitorBinding.inflate(getLayoutInflater());
        setContentView(activityWaterMonitorBinding.getRoot());
        // 初始化
        initData();
        initView();
    }

    private void initData() {
        mTitle = new ArrayList<>();
        mTitle.add("实时数据");
        mTitle.add("安装检查");
        mTitle.add("参数设置");
        mTitle.add("联合调试");
        if (mFragment==null){
            mFragment = new ArrayList<>();
            mFragment.add(new LiveDataFragment());
            mFragment.add(new InstallationFragment());
            mFragment.add(new ParameterSetFragment());
            mFragment.add(new JointDebugFragment());
        }

    }

    private void initView(){
        mTabLayout = findViewById(R.id.mTabLayout);
        mViewPager = findViewById(R.id.mViewPager);

        // 预加载
        mViewPager.setOffscreenPageLimit(mFragment.size());

        //mViewPaper 滑动监听
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.i("TAG","position:" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        // 设置适配器
        FragmentManager fragmentManager = getSupportFragmentManager();
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragment.get(position);
            }

            @Override
            public int getCount() {
                return mFragment.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return mTitle.get(position);
            }
        });

        // 绑定
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void onClick(View view) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }

}
